package com.jerry.constellation.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.jerry.constellation.R;
import com.jerry.constellation.Utils.AssetsUtils;
import com.jerry.constellation.Utils.LoadDataDetialTask;
import com.jerry.constellation.Utils.URLContent;
import com.jerry.constellation.bean.PartnerDetialsBean;

import java.net.URL;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class PartnerDetialsActivity extends AppCompatActivity implements LoadDataDetialTask.OnGetNetDataListener {

    private CircleImageView maleicon;
    private CircleImageView femaleicon;
    private TextView male_name;
    private TextView female_name;
    private TextView match;
    private TextView vs;
    private TextView mark;
    private TextView weight;
    private TextView analysis;
    private TextView attention;
    private ImageView back;
    //get data
    private String male_name1;
    private String female_name1;
    private String female_logoname;
    private String male_logoname;
    private TextView title_tv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partnerdetials);
        initView();
        //获取传送的值
        getDatafromLast();
        initData();
    }

    private void initData() {
        //加载本地数据
        Map<String, Bitmap> contentlogimgMap = AssetsUtils.getContentlogimgMap();
        Bitmap malebitmap = contentlogimgMap.get(male_logoname);
        Bitmap femalebitmap = contentlogimgMap.get(female_logoname);
        maleicon.setImageBitmap(malebitmap);
        femaleicon.setImageBitmap(femalebitmap);
        male_name.setText(male_name1);
        female_name.setText(female_name1);
        match.setText("星座配对:****"+male_name1+"和"+female_name1+"****");
        vs.setText(male_name1+"VS"+female_name1);
        //加载网络数据
        //获取网络路程地址
        String url = URLContent.getPartnerURL(male_name1, female_name1);
        //1.创建自定义的异步任务的对象
        LoadDataDetialTask task = new LoadDataDetialTask(this, this, true);
        //2.执行异步任务
        task.execute(url);
    }
    @Override
    public void onSuccess(String json) {
        //解析数据
        if (!TextUtils.isEmpty(json)) {
            PartnerDetialsBean detialsBean = new Gson().fromJson(json, PartnerDetialsBean.class);
            PartnerDetialsBean.ResultBean resultBean = detialsBean.getResult();

            mark.setText("配对评分："+resultBean.getZhishu()+" "+resultBean.getJieguo());
            weight.setText("星座比重："+resultBean.getBizhong());
            analysis.setText("解析：\n\n"+resultBean.getLianai());
            attention.setText("注意事项：\n\n"+resultBean.getZhuyi());
        }
    }

    private void getDatafromLast() {
        Intent intent = getIntent();
        male_name1 = intent.getStringExtra("male_name");
        male_logoname = intent.getStringExtra("male_logoname");
        female_name1 = intent.getStringExtra("female_name");
        female_logoname = intent.getStringExtra("female_logoname");

    }

    private void initView() {
        maleicon = findViewById(R.id.partnerDetials_maleicon);
        femaleicon = findViewById(R.id.partnerDetials_femaleicon);
        male_name = findViewById(R.id.partnerDetials_Tv_male);
        female_name = findViewById(R.id.partnerDetials_Tv_female);
        title_tv = findViewById(R.id.title_tv);
        title_tv.setText("配对详情");
        //显示文本框
        match = findViewById(R.id.partnerDetials_Tv_pd);
        vs = findViewById(R.id.partnerDetials_Tv_vs);
        mark = findViewById(R.id.partnerDetials_Tv_pf);
        weight = findViewById(R.id.partnerDetials_Tv_bz);
        analysis = findViewById(R.id.partnerDetials_Tv_jx);
        attention = findViewById(R.id.partnerDetials_Tv_zy);
        //返回键
        back = findViewById(R.id.title_iv_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


}
